<template>
  <div class="app-container">
    <!-- 搜索区 -->
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="商家名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商家名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dicts.aduit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="btnSearch">搜索</el-button>
      </el-form-item>
    </el-form>

    <!--功能区-->
    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          v-requiredMerchant
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate()"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport()"
        >导出</el-button>
      </el-col>
    </el-row>

    <!-- 数据展示区table-->
    <el-table v-loading="loading" :data="tableData.rows" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="el-form-item">
                <label class="el-form-item__label">商家账户</label>
                <div class="el-form-item__content">
                  <span>{{ scope.row.loginName }}</span>
                </div>
              </div>
            </el-col>

            <el-col :span="8" />

            <el-col :span="8" />

            <el-col :span="24">
              <div class="el-form-item">
                <label class="el-form-item__label">商家地址</label>
                <div class="el-form-item__content">
                  <span>{{ scope.row.address }}</span>
                </div>
              </div>
            </el-col>

            <el-col :span="24">
              <div class="el-form-item">
                <label class="el-form-item__label">商家位置</label>
                <div class="el-form-item__content">
                  <BaiduDisplay :item-data="scope.row" />
                </div>
              </div>
            </el-col>

          </el-row>
        </template>
      </el-table-column>

      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家ID" prop="id" width="100" />
      <el-table-column label="商家名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="联系电话" prop="phone" />
      <el-table-column label="分类图片" width="160">
        <template slot-scope="scope">
          <img :src="scope.row.picture" width="150">
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" width="200">
        <template slot-scope="scope">
          <!-- 商家显示-->
          <div v-if="usertype===1">
            <el-tag v-if="scope.row.status===0" type="success">已审核</el-tag>
            <el-tag v-else type="danger">未审核</el-tag>
          </div>
          <!-- 管理员显示 -->
          <el-switch
            v-if="usertype==0"
            v-model="scope.row.status"
            style="display: block"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="已审核"
            :active-value="0"
            inactive-text="未审核"
            :inactive-value="1"
          />

        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-requiredMerchant
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页条 -->
    <div style="margin-top: 10px;">
      <el-pagination
        v-if="tableData.total>0"
        :current-page="queryParams.page"
        :page-sizes="[10, 20, 30, 40,50,100]"
        :page-size="queryParams.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!--  修改对话框-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="form.loginName" readonly placeholder="请输入登录名称(唯一)" />
        </el-form-item>

        <el-form-item label="商家名称" prop="name">
          <el-input v-model="form.name" placeholder="商家名称" />
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="联系电话" />
        </el-form-item>

        <el-form-item label="地图位置" prop="longitude">
          <el-button type="primary" @click="handlerMapLocation()">选择位置</el-button>
        </el-form-item>

        <el-form-item label="商家地址" prop="address">
          <el-input v-model="form.address" placeholder="商家地址" />
        </el-form-item>

        <el-form-item label="商家图像" prop="picture">
          <el-upload
            class="avatar-uploader"
            :action="imageUploadUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.picture" :src="form.picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>

    </el-dialog>

    <!-- 地图选择器-->
    <el-dialog title="位置选择器" :visible.sync="mapShow" width="800px" append-to-body :close-on-click-modal="false">

      <BaiduAddress v-if="form.id" :item-data="form" @myclick="handlerLocation" />
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import DictDataMixin from '@/mixin/DictDataMixin.js'
import PageMixin from '@/mixin/PageMixin.js'
import AddOrEditMixin from '@/mixin/AddOrEditMixin.js'
import { dataApi, addApi, editApi, findByIdApi, deleteByIdApi } from '@/api/merchandiser.js'
import BaiduDisplay from '@/components/BaiduAddress/display.vue'
import BaiduAddress from '@/components/BaiduAddress/index.vue'
export default {
  computed: {
    ...mapGetters([
      'usertype'
    ])
  },
  components: {
    BaiduDisplay,
    BaiduAddress
  },
  mixins: [DictDataMixin, PageMixin, AddOrEditMixin],
  // 查询通字典的2个对象的值
  dictkeys: ['aduit'],
  data() {
    return {
      fetchDataApi: dataApi,
      editApi: editApi,
      findByIdApi: findByIdApi,
      deleteByIdApi: deleteByIdApi,
      primaryKey: 'id',
      // 是否显示地图选择框
      mapShow: false,
      rules: {
      }
    }
  },
  created() {
    this.initFormValue()
  },
  methods: {
    initFormValue() {
      this.form = {
        picture: undefined
      }
    },
    handleExport() {
      this.download('/api/merchandiser/excel', this.queryParams, `商家信息_${new Date().getTime()}.xlsx`)
    },
    // 图片上传成功以后返回的数据(json, file[blob]数据)
    handleAvatarSuccess(res, file) {
      this.form.picture = res.data.url
    },
    // 地图位置注册事件回调
    handlerLocation(resp) {
      console.log(resp)
      this.form.address = resp.address
      this.form.longitude = resp.longitude
      this.form.latitude = resp.latitude
      this.mapShow = false
    },
    // 加载地图定位模块
    handlerMapLocation() {
      this.mapShow = true
    }
  }
}
</script>

<style>
</style>
